package cn.zlj.nmcc.web.controller;

import cn.zlj.nmcc.domain.UserGrowLog;
import cn.zlj.nmcc.domain.UserIntegralLog;
import cn.zlj.nmcc.dto.IntegralDto;
import cn.zlj.nmcc.mapper.UserBaseInfoMapper;
import cn.zlj.nmcc.service.IUserBaseInfoService;
import cn.zlj.nmcc.domain.UserBaseInfo;
import cn.zlj.nmcc.query.UserBaseInfoQuery;
import cn.zlj.nmcc.result.JSONResult;
import cn.zlj.nmcc.result.PageList;
import cn.zlj.nmcc.service.IUserGrowLogService;
import cn.zlj.nmcc.service.IUserIntegralLogService;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/userBaseInfo")
public class UserBaseInfoController {

    @Autowired
    public IUserBaseInfoService userBaseInfoService;

    @Autowired
    private IUserIntegralLogService userIntegralLogService;

    @Autowired
    private IUserGrowLogService userGrowLogService;
    @Autowired
    private UserBaseInfoMapper userBaseInfoMapper;
    @Transactional
    @RequestMapping(value = "/recharge", method = RequestMethod.POST)
    public JSONResult rechargeOrSpend(@RequestBody IntegralDto integralDto){
        UserIntegralLog userIntegralLog = new UserIntegralLog();
        UserGrowLog userGrowLog = new UserGrowLog();
        UserBaseInfo userBaseInfo = userBaseInfoMapper.selectById(integralDto.getId());
        //积分
        Integer integralScore = userBaseInfo.getIntegralScore();
        //成长值
        Integer growScore = userBaseInfo.getGrowScore();
        //状态为0是充值 1是花费
        if(integralDto.getStatus()==0){
            userBaseInfo.setIntegralScore(integralScore+integralDto.getScore());
            userBaseInfo.setGrowScore(growScore+integralDto.getScore());
            extracted(integralDto, userIntegralLog);
            extracted(integralDto, userGrowLog);
            userGrowLogService.insert(userGrowLog);
        }else {
            userBaseInfo.setIntegralScore(integralScore-integralDto.getScore());
            extracted1(integralDto, userIntegralLog);
        }
        userBaseInfoService.updateById(userBaseInfo);

        return JSONResult.success();
    }

    private void extracted1(@RequestBody IntegralDto integralDto, UserIntegralLog userIntegralLog) {
        userIntegralLog.setScore(integralDto.getScore().longValue());
        userIntegralLog.setNote("花费了"+ integralDto.getScore()+"积分");
        userIntegralLog.setUserId(integralDto.getId());
        userIntegralLogService.insert(userIntegralLog);
    }

    private void extracted(IntegralDto integralDto, UserGrowLog userGrowLog) {
        userGrowLog.setScore(integralDto.getScore());
        userGrowLog.setRemark("充值增加:"+ integralDto.getScore()+"成长值");
        userGrowLog.setCreateTime(new Date().getTime());
        userGrowLog.setUserId(integralDto.getId());
    }

    private void extracted(IntegralDto integralDto, UserIntegralLog userIntegralLog) {
        userIntegralLog.setScore(integralDto.getScore().longValue());
        userIntegralLog.setNote("充值赠送了"+ integralDto.getScore()+"积分");
        userIntegralLog.setUserId(integralDto.getId());
        userIntegralLogService.insert(userIntegralLog);
    }

    /**
     * 保存和修改公用的
     */
    //充值或者花费积分Recharge
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @Transactional
    public JSONResult saveOrUpdate(@RequestBody UserBaseInfo userBaseInfo) {
        UserIntegralLog userIntegralLog = new UserIntegralLog();
        UserGrowLog userGrowLog = new UserGrowLog();

        if (userBaseInfo.getId() != null) {
            userBaseInfoService.updateById(userBaseInfo);
        } else {
            userBaseInfoService.insert(userBaseInfo);
            //注册赠送成长值
            extracted(userBaseInfo, userGrowLog);
            //注册赠送积分
            extracted(userBaseInfo, userIntegralLog);

        }
        return JSONResult.success();
    }

    private void extracted(UserBaseInfo userBaseInfo, UserIntegralLog userIntegralLog) {
        userIntegralLog.setScore(10L);
        userIntegralLog.setNote("注册成功送10积分");
        userIntegralLog.setUserId(userBaseInfo.getId());
        userIntegralLogService.insert(userIntegralLog);
    }

    private void extracted(UserBaseInfo userBaseInfo, UserGrowLog userGrowLog) {
        userGrowLog.setScore(10);
        userGrowLog.setRemark("注册成功送10成长值");
        userGrowLog.setCreateTime(new Date().getTime());
        userGrowLog.setUserId(userBaseInfo.getId());
        userGrowLogService.insert(userGrowLog);
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id) {
        userBaseInfoService.deleteById(id);
        return JSONResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id") Long id) {
        return JSONResult.success(userBaseInfoService.selectById(id));
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JSONResult list() {
        return JSONResult.success(userBaseInfoService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JSONResult page(@RequestBody UserBaseInfoQuery query) {
        Page<UserBaseInfo> page = new Page<UserBaseInfo>(query.getPage(), query.getRows());
        page = userBaseInfoService.selectPage(page);
        return JSONResult.success(new PageList<UserBaseInfo>(page.getTotal(), page.getRecords()));
    }
}
